package com.tidc.seaserverservice.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tidc.seaservermapper.dao.CommitmentDao;
import com.tidc.seaservermapper.dao.RabbitMessageLogDao;
import com.tidc.seaservermodel.config.RabbitConfig;
import com.tidc.seaservermodel.entity.Commitment;
import com.tidc.seaservermodel.entity.Email;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.RabbitMessageLog;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.util.SpringUtil;
import com.tidc.seaserverservice.service.SupervisorCommitmentService;
import com.tidc.seaserverservice.util.RabbitUtil;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/10/7 21:04
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class SupervisorCommitmentServiceImpl implements SupervisorCommitmentService {
    @Resource
    private CommitmentDao commitmentDao;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RabbitMessageLogDao rabbitMessageLogDao;
    @Resource
    private RabbitUtil rabbitUtil;
    @Override
    public Fres<Map<Object, Object>> getCommitmentList(int type, int pageSize, int pageNum, Authentication authentication) throws TidcBadParent {
        int count = commitmentDao.getSupervisorCommitCount(type,Integer.parseInt(authentication.getName()));
        Map<Object, Object> map = SpringUtil.pagingPrepare(pageSize, pageNum, count);
        List<Commitment> commitments = commitmentDao.supervisorGetList(type,(pageNum - 1) * pageSize, pageSize, Integer.parseInt(authentication.getName()));
        map.put("list",commitments);
        return Fres.Success(map,"成功");
    }
    @Transactional
    @Override
    public Fres<String> fulfillCommitment(Commitment commitment,Authentication authentication) throws JsonProcessingException {
        commitment = commitmentDao.queryById(commitment.getId());
        commitmentDao.SupervisorFulfillCommitment(commitment.getId(), Integer.parseInt(authentication.getName()));
        Email email = Email.supervisorFulfillCommitment(commitment);
        RabbitMessageLog rabbitMessageLog = RabbitMessageLog.sendEmail(objectMapper.writeValueAsString(email));
        rabbitUtil.sendMQ(rabbitMessageLog);
        return Fres.Success();
    }
}
